import axios from 'axios';
import Qs from 'qs';
import {Message, MessageBox} from 'element-ui';

let access =  window.basePath;

//axios.defaults.withCredentials = true;
//全局设置axios
axios.defaults.validateStatus = function (status) {
    return status <= 500; // Reject only if the status code is greater than or equal to 500
};

//格式化formData
function stringify(data) {
    data = Qs.stringify(data);
    return data;
}
//formData配置
let form_config = {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
}

//公共回调处理
function axios_ajax(fn, callback, error) {
    fn.then((data) => {
        if (data.status == 200) {
            if (window.location.hash != '#/login') {
                if (data.data.code) {
                    if (data.data.code == 3001) {
                        MessageBox.alert('登录超时', '提示', {
                            type: 'warning',
                            confirmButtonText: '重新登录',
                            callback: action => {
                                sessionStorage.removeItem('user');
                                window.location.hash = '#/login';
                            }
                        });
                        return;
                    }
                }
            }
            callback(data);
        } else {
            error(data);
        }
    }).catch((err) => {
        error(err);
        Message.error('网络或服务异常');
    });
}


export default {
    //获取配置
    getData:(params, callback, error) => {
        axios_ajax(axios.get(`${access}/getsetting`, params), callback, error);
    },
    //提交配置
    postData: (params, callback, error) => {
        axios_ajax(axios.post(`${access}/setsetting`, params), callback, error);
    },

    //获取对外接口列表
    getSettingData: (params, callback, error) => {
        axios_ajax(axios.get(`${access}/getSettingData`, params), callback, error);
    }
}



